package com.feihong.yw189.push;

import android.content.Context;
import android.content.Intent;

import com.blankj.utilcode.util.LogUtils;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.yonghuzhongxin.wodexiaoxi.MessageListActivity;
import com.feihong.yw189.utils.PreferenceTools;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;
import java.util.HashMap;
import java.util.List;

/**
 * @author wangziheng
 * @name Android  小米消息广播
 * @class name：com.fhdz.ywydt.push
 * @class describe:
 * @time 2018/1/10 10:48
 * @change
 * @chang time
 * @class describe
 */
public class MIPushReceiver extends PushMessageReceiver{

    private String mRegId;

    private String TAG = "营维189";
    private HashMap<String, String> mMsgID = new HashMap<>();
    public static final String MIPUSH_REGIN_ID = "REGIN_ID";
    /**
     * 通知栏点击
     * @param context
     * @param message
     */
    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message) {

        String mMsgId = message.getMessageId();
        mMsgID.remove(mMsgId);
        Intent intent = new Intent(context, MessageListActivity.class);
        intent.toUri(Intent.URI_INTENT_SCHEME);
        intent.putExtra("msgId",mMsgId);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 接收到消息
     * @param context
     * @param message
     */
    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        LogUtils.i(TAG + "  "+ message.toString());
    }

    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
                PreferenceTools.putString(MIPUSH_REGIN_ID,mRegId);
                LogUtils.i(TAG + " regId : " + mRegId);
            } else {
                LogUtils.i(TAG + "推送id获取失败");
            }
        }
    }
}
